#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

void STInit(ST* ps)
{
	assert(ps);

	ps->a=(STDataType*)malloc(sizeof(STDataType)*4);
	if(ps->a==NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->top = 0;
	ps->capacity = 4;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void STPush(ST* ps,STDataType x)
{
	assert(ps);

	if (ps->top==ps->capacity)
	{
		STDataType* temp=(STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
		if (temp==NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = temp;
		ps->capacity = ps->capacity * 2;
	}

	ps->a[ps->top] = x;
	ps->top++;
}


bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top==0;
}
void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	ps->top--;
}

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

STDataType STTop(ST* ps)
{ 
	assert(ps);
	assert(!STEmpty(ps));

	return ps->a[ps->top-1];
}